package com.snow.useviewdemo.test.rxjava

import com.snow.useviewdemo.util.LogUtils
import rx.Observable
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers
import rx.functions.Func1

/**
 * Created by zhaoyingtao
 * Date: 2021/8/27
 * Describe:
 */
class RxJavaMergeTest private constructor() {
    companion object {
        //        private var instance: RxJavaMergeTest? = null
//            get() {
//                if (field == null) {
//                    field = RxJavaMergeTest()
//                }
//                return field
//            }
//
//        fun get(): RxJavaMergeTest {
//            return instance!!
//        }
        val instance: RxJavaMergeTest by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            RxJavaMergeTest()
        }
    }

    fun testMapAndFlatMap() {
        val courses1: MutableList<Course> = ArrayList()

        courses1.add(Course("1"))
        courses1.add(Course("2"))
        courses1.add(Course("3"))

        val student1 = Student()
        student1.name = "student1"
        student1.courseList = courses1

        val courses2: MutableList<Course> = ArrayList()

        courses2.add(Course("4"))
        courses2.add(Course("5"))
        courses2.add(Course("6"))

        val student2 = Student()
        student2.name = "student2"
        student2.courseList = courses2

        val students: MutableList<Student> = ArrayList()
        students.add(student1)
        students.add(student2)
        observableDefault(students)
        observableMap(students)
        observableFlatMap(students)
    }

    private fun observableDefault(students: MutableList<Student>) {
        Observable.from(students).observeOn(AndroidSchedulers.mainThread()).subscribe(object : Subscriber<Student?>(){
            override fun onCompleted() {
                LogUtils.e("onCompleted()=======1")
            }

            override fun onError(e: Throwable?) {
                LogUtils.e("onError()=======1")
            }

            override fun onNext(student: Student?) {
                for (i in student?.courseList?.indices!!) {
                    LogUtils.e("onNext:" + student.courseList[i].name)
                }
            }

        })
    }
    private fun observableMap(students: MutableList<Student>){
        Observable.from(students)
                .map(Func1<Student,List<Course>>{
                    return@Func1 it.courseList
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object :Subscriber<List<Course>>(){
                    override fun onCompleted() {
                        LogUtils.e("onCompleted()=======map")
                    }

                    override fun onError(e: Throwable?) {
                        LogUtils.e("onError()=======map")
                    }

                    override fun onNext(t: List<Course>?) {
                        if (t != null) {
                            for (course in t){
                                LogUtils.e("onNext:map===" + course.name)
                            }
                        }
                    }

                })
    }
    private fun observableFlatMap(students: MutableList<Student>){
        Observable.from(students)
                .flatMap(Func1<Student,Observable<Course>>{
                    return@Func1 Observable.from(it.courseList)
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object :Subscriber<Course>(){
                    override fun onCompleted() {
                        LogUtils.e("onCompleted()=======flatmap")
                    }

                    override fun onError(e: Throwable?) {
                        LogUtils.e("onError()=======flatmap")
                    }

                    override fun onNext(course: Course?) {
                        LogUtils.e("onNext:flatmap===" + course?.name)
                    }

                })
    }
}